import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../public_util.dart';
import 'action.dart';
import 'state.dart';

List lableArr = ['家', '公司', '学校'];

Widget buildView(IntegralAddressEditState state, Dispatch dispatch,
    ViewService viewService) {
  return GestureDetector(
    onTap: () {
      FocusScope.of(viewService.context).requestFocus(FocusNode());
    },
    child: Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 248, 1),
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        title: Text(
          state.title,
          style: TextStyle(
              color: Color.fromRGBO(47, 47, 47, 1),
              fontSize: setSp(16),
              fontWeight: FontWeight.bold),
        ),
        leading: GestureDetector(
            onTap: () {
              Map dic = {'title':'新增收货地址'};
              FocusScope.of(viewService.context).requestFocus(FocusNode());
              Navigator.of(viewService.context).pop(dic);
            },
            child: Icon(Icons.arrow_back_ios_rounded,
                color: Color.fromRGBO(47, 47, 47, 1)),),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0.0,
        brightness: Brightness.light,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: setWidth(20),
            ),
            Column(
              children: [
                Container(
                  color: Colors.white,
                  height: setWidth(40),
                  padding: EdgeInsets.symmetric(
                    horizontal: setWidth(15),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: setWidth(55),
                        child: Text(
                          '收货人',
                          style: TextStyle(
                            color: Color.fromRGBO(153, 153, 153, 1),
                            fontSize: setSp(13),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(12),
                      ),
                      Expanded(
                        child: TextField(
                          controller: state.nameController,
                          maxLines: 1,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                              fontSize: setSp(13),
                              color: Color.fromRGBO(47, 47, 47, 1)),
                          onChanged: (text) {
                            print('change $text');
                            dispatch(
                                IntegralAddressEditActionCreator.upName(text));
                          },

                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: '请输入收货人姓名',
                            hintStyle: TextStyle(
                                color: Color.fromRGBO(200, 200, 201, 1),
                                fontSize: setSp(13)),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: setWidth(0.5),
                  color: Color.fromRGBO(238, 238, 238, 1),
                )
              ],
            ),
            Column(
              children: [
                Container(
                  color: Colors.white,
                  height: setWidth(40),
                  padding: EdgeInsets.symmetric(
                    horizontal: setWidth(15),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: setWidth(55),
                        child: Text(
                          '手机号码',
                          style: TextStyle(
                            color: Color.fromRGBO(153, 153, 153, 1),
                            fontSize: setSp(13),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(12),
                      ),
                      Expanded(
                        child: TextField(
                          controller: state.phoneController,
                          maxLines: 1,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                              fontSize: setSp(13),
                              color: Color.fromRGBO(47, 47, 47, 1)),
                          onChanged: (text) {
                            print('change $text');
                            dispatch(
                                IntegralAddressEditActionCreator.upPhone(text));
                          },
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: '请输入手机号码',
                            hintStyle: TextStyle(
                                color: Color.fromRGBO(200, 200, 201, 1),
                                fontSize: setSp(13)),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  height: setWidth(0.5),
                  color: Color.fromRGBO(238, 238, 238, 1),
                )
              ],
            ),
            Column(
              children: [
                Container(
                  color: Colors.white,
                  height: setWidth(40),
                  padding: EdgeInsets.symmetric(
                    horizontal: setWidth(15),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: setWidth(55),
                        child: Text(
                          '所在地区',
                          style: TextStyle(
                            color: Color.fromRGBO(153, 153, 153, 1),
                            fontSize: setSp(13),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(12),
                      ),
                      Expanded(
                        child: GestureDetector(
                          onTap: () {},
                          child: Text(
                            '请选择地区',
                            style: TextStyle(
                              color: Color.fromRGBO(200, 200, 201, 1),
                              fontSize: setSp(13),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  height: setWidth(0.5),
                  color: Color.fromRGBO(238, 238, 238, 1),
                ),
              ],
            ),
            Container(
              color: Colors.white,
              height: setWidth(81),
              padding: EdgeInsets.symmetric(
                horizontal: setWidth(15),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(
                      top: setWidth(12),
                    ),
                    width: setWidth(67),
                    child: Text(
                      '详细地址：',
                      style: TextStyle(
                        color: Color.fromRGBO(153, 153, 153, 1),
                        fontSize: setSp(13),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(
                        top: setWidth(1),
                      ),
                      child: TextField(
                        controller: state.addressController,
                        keyboardType: TextInputType.multiline,
                        maxLines: 5,
                        minLines: 1,
                        textAlign: TextAlign.left,
                        style: TextStyle(
                            fontSize: setSp(13),
                            color: Color.fromRGBO(47, 47, 47, 1)),
                        onChanged: (text) {
                          print('change $text');
                          dispatch(
                              IntegralAddressEditActionCreator.upAddress(text));
                        },
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: '如道路、门派号、小区、楼栋号、单元室等',
                          hintStyle: TextStyle(
                              color: Color.fromRGBO(200, 200, 201, 1),
                              fontSize: setSp(13)),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: setWidth(20),
            ),
            Column(
              // crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  color: Colors.white,
                  height: setWidth(42),
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(
                    horizontal: setWidth(15),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: setWidth(55),
                        child: Text(
                          '地址标签',
                          style: TextStyle(
                            color: Color.fromRGBO(47, 47, 47, 1),
                            fontSize: setSp(13),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(12),
                      ),
                      Row(
                        children: lableArr
                            .asMap()
                            .map((index, value) {
                              bool isSelectLb = index == state.indexLb;
                              return MapEntry(
                                index,
                                GestureDetector(
                                  onTap: () {
                                    dispatch(IntegralAddressEditActionCreator
                                        .upSelectLb(index));
                                  },
                                  child: Container(
                                    height: setWidth(24),
                                    padding: EdgeInsets.symmetric(
                                        horizontal: setWidth(14)),
                                    margin:
                                        EdgeInsets.only(right: setWidth(12)),
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                          color: isSelectLb
                                              ? Color.fromRGBO(255, 37, 38, 1)
                                              : Color.fromRGBO(
                                                  163, 163, 163, 1),
                                          width: 0.5),
                                      color: isSelectLb
                                          ? Color.fromRGBO(255, 37, 38, 1)
                                          : Colors.white, // 底色
                                      borderRadius: new BorderRadius.circular(
                                          setWidth(24)),
                                    ),
                                    child: Text(
                                      value,
                                      style: TextStyle(
                                        color: isSelectLb
                                            ? Colors.white
                                            : Color.fromRGBO(153, 153, 153, 1),
                                        fontSize: setSp(13),
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            })
                            .values
                            .toList(),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: setWidth(0.5),
                  color: Color.fromRGBO(238, 238, 238, 1),
                )
              ],
            ),
            Container(
              color: Colors.white,
              height: setWidth(42),
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(
                horizontal: setWidth(15),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    child: Text(
                      '设置为默认地址',
                      style: TextStyle(
                        color: Color.fromRGBO(47, 47, 47, 1),
                        fontSize: setSp(13),
                      ),
                    ),
                  ),
                  Transform.scale(
                    scale: 0.7,
                    child: Container(
                      width: 50,
                      height: 20,
                      child: CupertinoSwitch(
                        value: state.valueSwitch,
                        onChanged: (newValue) {
                          dispatch(IntegralAddressEditActionCreator.upSwitch(
                              newValue));
                        },
                        activeColor: Color.fromRGBO(255, 37, 38, 1),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: setWidth(40),
            ),
            GestureDetector(
              onTap: () {
                if (!isNotEmptyString(state.name)) {
                  toast('请输入收货人姓名');
                  return;
                }
                if (!isNotEmptyString(state.phone)) {
                  toast('请输入手机号码');
                  return;
                }
                if (!isNotEmptyString(state.area)) {
                  toast('请选择地区');
                  return;
                }
                if (!isNotEmptyString(state.address)) {
                  toast('请输入详细地址');
                  return;
                }
              },
              child: Container(
                height: setWidth(45),
                margin:
                    EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Color.fromRGBO(255, 37, 38, 1),
                  borderRadius: BorderRadius.circular(setWidth(45)),
                ),
                child: Text(
                  '保存',
                  style: TextStyle(
                    fontSize: setSp(15),
                    color: Colors.white,
                  ),
                ),
              ),
            ),
            Offstage(
              offstage: false,
              child: GestureDetector(
                onTap: () async {
                  _showDialog(state, dispatch, viewService);
                },
                child: Container(
                  height: setWidth(45),
                  margin: EdgeInsets.only(
                    left: setWidth(15),
                    right: setWidth(15),
                    top: setWidth(12),
                  ),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(204, 204, 204, 1),
                    borderRadius: BorderRadius.circular(setWidth(45)),
                  ),
                  child: Text(
                    '删除',
                    style: TextStyle(
                      fontSize: setSp(15),
                      color: Color.fromRGBO(136, 136, 136, 1),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

// 弹出对话框
_showDialog(IntegralAddressEditState state, Dispatch dispatch,
    ViewService viewService) async {
  showDialog(
    context: viewService.context,
    builder: (context) {
      return Center(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: setWidth(48)),
          height: setWidth(148),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(setWidth(8)),
          ),
          child: Column(
            children: [
              Container(
                padding:
                    EdgeInsets.only(top: setWidth(30), bottom: setWidth(40)),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(setWidth(8)),
                ),
                child: Text(
                  '确定删除收收货地址吗',
                  style: TextStyle(
                      color: Color.fromRGBO(47, 47, 47, 1),
                      fontSize: setSp(15),
                      decoration: TextDecoration.none),
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                  left: setWidth(20),
                  right: setWidth(20),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                      child: Container(
                        height: setWidth(37),
                        padding: EdgeInsets.symmetric(horizontal: setWidth(41)),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(setWidth(37)),
                          color: Color.fromRGBO(238, 238, 238, 1),
                        ),
                        child: Text(
                          '取消',
                          style: TextStyle(
                              color: Color.fromRGBO(153, 153, 153, 1),
                              fontSize: setSp(15),
                              decoration: TextDecoration.none),
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                      child: Container(
                        height: setWidth(37),
                        padding: EdgeInsets.symmetric(horizontal: setWidth(41)),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(setWidth(37)),
                          color: Color.fromRGBO(255, 37, 38, 1),
                        ),
                        child: Text(
                          '确定',
                          style: TextStyle(
                              color: Color.fromRGBO(255, 255, 255, 1),
                              fontSize: setSp(15),
                              decoration: TextDecoration.none),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    },
  );
}
